import Vue from 'vue'

import Router from 'vue-router'



Vue.use(Router);



const router = new Router({
    routes: [
        { path: '/', redirect: '/login' },
        {
            path: '/login',
            component: () => import('../views/login/login.vue')
        },
        {
            path: '/home',
            component: () => import('../views/from/home.vue'),
            redirect: '/home/welcome',
            children: [
                { path: 'welcome', component: () => import('../views/main/Welcome.vue'), },
                { path: 'users', component: () => import('../views/main/Users.vue'), },
                { path: 'roles', component: () => import('../views/main/Roles.vue'), },
                { path: 'rights', component: () => import('../views/main/Rights.vue'), },
                {
                    path: 'goods', component: () => import('../views/main/Goods.vue'),
                },
                { path: 'params', component: () => import('../views/main/Params.vue'), },
                { path: 'categories', component: () => import('../views/main/Categories.vue'), },
                { path: 'orders', component: () => import('../views/main/Orders.vue'), },
                { path: 'reports', component: () => import('../views/main/Reports.vue'), },
                { path: 'goods/add', component: () => import('../views/main/Add.vue'), },

            ]
        }

    ]
})
//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
    // console.log('前置路由守卫', to, from)
    if (to.path == '/login') return next()
    const tokenStr = sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

export default router